#include "oled.h"
#include "i2c.h"
#include "delay.h"

const unsigned char F8X16[] = {
    // 空格 (ASCII 32)
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ! (ASCII 33)
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // " (ASCII 34)
    0x00,0x00,0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00,
    
    // # (ASCII 35)
    0x00,0x00,0x14,0x7F,0x14,0x14,0x7F,0x14,0x14,0x7F,0x14,0x14,0x00,0x00,0x00,0x00,
    
    // $ (ASCII 36)
    0x00,0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // % (ASCII 37)
    0x00,0x00,0x46,0x26,0x10,0x08,0x04,0x32,0x52,0x08,0x08,0x08,0x00,0x00,0x00,0x00,
    
    // & (ASCII 38)
    0x00,0x00,0x30,0x48,0x48,0x30,0x4A,0x44,0x44,0x3A,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ' (ASCII 39)
    0x00,0x00,0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ( (ASCII 40)
    0x00,0x00,0x00,0x00,0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ) (ASCII 41)
    0x00,0x00,0x00,0x00,0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // * (ASCII 42)
    0x00,0x00,0x22,0x14,0x08,0x7F,0x08,0x14,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // + (ASCII 43)
    0x00,0x00,0x08,0x08,0x08,0x7F,0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // , (ASCII 44)
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x00,
    
    // - (ASCII 45)
    0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // . (ASCII 46)
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,
    
    // / (ASCII 47)
    0x00,0x00,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 0 (ASCII 48)
    0x00,0x00,0x3E,0x41,0x49,0x49,0x49,0x49,0x41,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 1 (ASCII 49)
    0x00,0x00,0x00,0x01,0x01,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 2 (ASCII 50)
    0x00,0x00,0x71,0x49,0x49,0x49,0x49,0x49,0x49,0x46,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 3 (ASCII 51)
    0x00,0x00,0x41,0x49,0x49,0x49,0x49,0x49,0x49,0x36,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 4 (ASCII 52)
    0x00,0x00,0x0C,0x0A,0x09,0x09,0x7F,0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 5 (ASCII 53)
    0x00,0x00,0x4F,0x49,0x49,0x49,0x49,0x49,0x49,0x31,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 6 (ASCII 54)
    0x00,0x00,0x3E,0x49,0x49,0x49,0x49,0x49,0x49,0x30,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 7 (ASCII 55)
    0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 8 (ASCII 56)
    0x00,0x00,0x36,0x49,0x49,0x49,0x49,0x49,0x49,0x36,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // 9 (ASCII 57)
    0x00,0x00,0x06,0x49,0x49,0x49,0x49,0x49,0x49,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // : (ASCII 58)
    0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ; (ASCII 59)
    0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x00,
    
    // < (ASCII 60)
    0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // = (ASCII 61)
    0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // > (ASCII 62)
    0x00,0x00,0x00,0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ? (ASCII 63)
    0x00,0x00,0x02,0x01,0x01,0x51,0x09,0x09,0x09,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // @ (ASCII 64)
    0x00,0x00,0x3E,0x41,0x5D,0x55,0x5D,0x45,0x41,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // A (ASCII 65)
    0x00,0x00,0x40,0x44,0x44,0x7E,0x44,0x44,0x7E,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // B (ASCII 66)
    0x00,0x00,0x7F,0x49,0x49,0x49,0x49,0x49,0x49,0x36,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // C (ASCII 67)
    0x00,0x00,0x3E,0x41,0x41,0x41,0x41,0x41,0x41,0x22,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // D (ASCII 68)
    0x00,0x00,0x7F,0x41,0x41,0x41,0x41,0x41,0x41,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // E (ASCII 69)
    0x00,0x00,0x7F,0x49,0x49,0x49,0x49,0x49,0x49,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // F (ASCII 70)
    0x00,0x00,0x7F,0x09,0x09,0x09,0x09,0x09,0x09,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // G (ASCII 71)
    0x00,0x00,0x3E,0x41,0x41,0x41,0x49,0x49,0x49,0x3A,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // H (ASCII 72)
    0x00,0x00,0x7F,0x08,0x08,0x08,0x08,0x08,0x08,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // I (ASCII 73)
    0x00,0x00,0x00,0x41,0x41,0x7F,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // J (ASCII 74)
    0x00,0x00,0x30,0x40,0x40,0x40,0x40,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // K (ASCII 75)
    0x00,0x00,0x7F,0x08,0x08,0x14,0x22,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // L (ASCII 76)
    0x00,0x00,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // M (ASCII 77)
    0x00,0x00,0x7F,0x02,0x04,0x08,0x04,0x02,0x04,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // N (ASCII 78)
    0x00,0x00,0x7F,0x02,0x04,0x08,0x10,0x20,0x40,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // O (ASCII 79)
    0x00,0x00,0x3E,0x41,0x41,0x41,0x41,0x41,0x41,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // P (ASCII 80)
    0x00,0x00,0x7F,0x09,0x09,0x09,0x09,0x09,0x09,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // Q (ASCII 81)
    0x00,0x00,0x3E,0x41,0x41,0x41,0x51,0x61,0x41,0x3E,0x40,0x00,0x00,0x00,0x00,0x00,
    
    // R (ASCII 82)
    0x00,0x00,0x7F,0x09,0x09,0x09,0x19,0x29,0x49,0x46,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // S (ASCII 83)
    0x00,0x00,0x26,0x49,0x49,0x49,0x49,0x49,0x49,0x32,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // T (ASCII 84)
    0x00,0x00,0x01,0x01,0x01,0x7F,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // U (ASCII 85)
    0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // V (ASCII 86)
    0x00,0x00,0x1F,0x20,0x40,0x40,0x40,0x40,0x20,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // W (ASCII 87)
    0x00,0x00,0x3F,0x40,0x40,0x30,0x30,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // X (ASCII 88)
    0x00,0x00,0x41,0x22,0x14,0x08,0x08,0x14,0x22,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // Y (ASCII 89)
    0x00,0x00,0x01,0x02,0x04,0x78,0x04,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // Z (ASCII 90)
    0x00,0x00,0x41,0x61,0x51,0x49,0x45,0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // [ (ASCII 91)
    0x00,0x00,0x00,0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // \ (ASCII 92)
    0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ] (ASCII 93)
    0x00,0x00,0x00,0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ^ (ASCII 94)
    0x00,0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // _ (ASCII 95)
    0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00,
    
    // ` (ASCII 96)
    0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // a (ASCII 97)
    0x00,0x00,0x20,0x54,0x54,0x54,0x54,0x54,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // b (ASCII 98)
    0x00,0x00,0x7F,0x48,0x44,0x44,0x44,0x44,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // c (ASCII 99)
    0x00,0x00,0x38,0x44,0x44,0x44,0x44,0x44,0x44,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // d (ASCII 100)
    0x00,0x00,0x38,0x44,0x44,0x44,0x44,0x48,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // e (ASCII 101)
    0x00,0x00,0x38,0x54,0x54,0x54,0x54,0x54,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // f (ASCII 102)
    0x00,0x00,0x08,0x7E,0x09,0x09,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // g (ASCII 103)
    0x00,0x00,0x98,0xA4,0xA4,0xA4,0xA4,0xA4,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // h (ASCII 104)
    0x00,0x00,0x7F,0x08,0x04,0x04,0x04,0x04,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // i (ASCII 105)
    0x00,0x00,0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // j (ASCII 106)
    0x00,0x00,0x40,0x40,0x40,0x40,0x3D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // k (ASCII 107)
    0x00,0x00,0x7F,0x10,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // l (ASCII 108)
    0x00,0x00,0x00,0x00,0x3F,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // m (ASCII 109)
    0x00,0x00,0x7C,0x04,0x04,0x78,0x04,0x04,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // n (ASCII 110)
    0x00,0x00,0x7C,0x08,0x04,0x04,0x04,0x04,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // o (ASCII 111)
    0x00,0x00,0x38,0x44,0x44,0x44,0x44,0x44,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // p (ASCII 112)
    0x00,0x00,0xFC,0x24,0x24,0x24,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // q (ASCII 113)
    0x00,0x00,0x18,0x24,0x24,0x24,0x24,0x24,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // r (ASCII 114)
    0x00,0x00,0x7C,0x08,0x04,0x04,0x04,0x04,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // s (ASCII 115)
    0x00,0x00,0x48,0x54,0x54,0x54,0x54,0x54,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // t (ASCII 116)
    0x00,0x00,0x04,0x04,0x3F,0x44,0x44,0x44,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // u (ASCII 117)
    0x00,0x00,0x3C,0x40,0x40,0x40,0x40,0x20,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // v (ASCII 118)
    0x00,0x00,0x1C,0x20,0x40,0x40,0x40,0x20,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // w (ASCII 119)
    0x00,0x00,0x3C,0x40,0x40,0x30,0x40,0x40,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // x (ASCII 120)
    0x00,0x00,0x44,0x28,0x10,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // y (ASCII 121)
    0x00,0x00,0x1C,0xA0,0xA0,0xA0,0xA0,0xA0,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // z (ASCII 122)
    0x00,0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // { (ASCII 123)
    0x00,0x00,0x08,0x08,0x36,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // | (ASCII 124)
    0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // } (ASCII 125)
    0x00,0x00,0x00,0x41,0x41,0x36,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    
    // ~ (ASCII 126)
    0x00,0x00,0x08,0x04,0x08,0x10,0x08,0x04,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

void OLED_WriteCmd(uint8_t cmd) {
    I2C_Start();
    I2C_SendByte(OLED_ADDRESS);
    I2C_SendByte(0x00);
    I2C_SendByte(cmd);
    I2C_Stop();
}

void OLED_WriteData(uint8_t data) {
    I2C_Start();
    I2C_SendByte(OLED_ADDRESS);
    I2C_SendByte(0x40);
    I2C_SendByte(data);
    I2C_Stop();
}

void OLED_SetPos(uint8_t x, uint8_t y) {
    OLED_WriteCmd(0xB0 + y);
    OLED_WriteCmd(((x & 0xF0) >> 4) | 0x10);
    OLED_WriteCmd(x & 0x0F);
}

void OLED_Init(void) {
    I2C_Soft_Init();  // 使用新函数名
    delay_ms(100);
    
    OLED_WriteCmd(0xAE); // 关闭显示
    OLED_WriteCmd(0xD5); // 设置时钟分频
    OLED_WriteCmd(0x80);
    OLED_WriteCmd(0xA8); // 设置复用率
    OLED_WriteCmd(0x3F);
    OLED_WriteCmd(0xD3); // 设置显示偏移
    OLED_WriteCmd(0x00);
    OLED_WriteCmd(0x40); // 设置显示起始行
    OLED_WriteCmd(0x8D); // 电荷泵设置
    OLED_WriteCmd(0x14);
    OLED_WriteCmd(0x20); // 内存地址模式
    OLED_WriteCmd(0x00);
    OLED_WriteCmd(0xA1); // 段重映射
    OLED_WriteCmd(0xC8); // 扫描方向
    OLED_WriteCmd(0xDA); // COM引脚配置
    OLED_WriteCmd(0x12);
    OLED_WriteCmd(0x81); // 对比度设置
    OLED_WriteCmd(0xCF);
    OLED_WriteCmd(0xD9); // 预充电周期
    OLED_WriteCmd(0xF1);
    OLED_WriteCmd(0xDB); // VCOMH设置
    OLED_WriteCmd(0x30);
    OLED_WriteCmd(0xA4); // 整个显示开启
    OLED_WriteCmd(0xA6); // 正常显示
    OLED_WriteCmd(0xAF); // 开启显示
    
    OLED_Clear();
}

void OLED_Clear(void) {
    uint8_t i, j;
    for(j=0; j<8; j++) {
        OLED_SetPos(0, j);
        for(i=0; i<128; i++) {
            OLED_WriteData(0x00);
        }
    }
}

void OLED_ShowChar(uint8_t x, uint8_t y, char ch, uint8_t size) {
    uint8_t c = ch - ' ';
    if(x > 120) {
        x = 0;
        y++;
    }
    
    if(size == 16) {
        OLED_SetPos(x, y);
        for(uint8_t i=0; i<8; i++) {
            OLED_WriteData(F8X16[c*16+i]);
        }
        OLED_SetPos(x, y+1);
        for(uint8_t i=0; i<8; i++) {
            OLED_WriteData(F8X16[c*16+i+8]);
        }
    }
}

void OLED_ShowString(uint8_t x, uint8_t y, const char *str, uint8_t size) {
    while(*str) {
        OLED_ShowChar(x, y, *str++, size);
        x += 8;
        if(x > 120) {
            x = 0;
            y += 2;
        }
    }
}

void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size) {
    char str[10];
    sprintf(str, "%d", num);
    OLED_ShowString(x, y, str, size);
}